package cn.chenyumeng.blog.controller.admin;

import cn.chenyumeng.blog.dto.CommentDTO;
import cn.chenyumeng.blog.dto.CommonResponse;
import cn.chenyumeng.blog.dto.ResultCode;
import cn.chenyumeng.blog.service.admin.CommentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @program: blog->CommentController
 * @description:
 * @author: cg
 * @create: 2020-07-27 16:52
 **/
@RestController
@ResponseBody
@RequestMapping("/admin/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/getCommentListByBlogIdAndLimit")
    public CommonResponse getCommentListByBlogIdAndLimit(@NotNull(message = "博客Id不能为空") Long id,
                                                         @RequestParam(name = "page", defaultValue = "1") Integer page,
                                                         @RequestParam(name = "size", defaultValue = "8") Integer size) {
        PageInfo<List<CommentDTO>> pageInfo = commentService.getCommentListByBlogId(id, page, size);
        if (pageInfo == null) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok(pageInfo);
    }

    @GetMapping("/deleteCommentByCommentId")
    public CommonResponse deleteCommentByCommentId(@NotNull(message = "评论Id不能为空") Long id) {
        if (!commentService.deleteById(id)) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok();
    }

}
